package cn.calm.swing.osgi.host.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.SwingConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;

import cn.calm.swing.osgi.api.ProjectPersistenceService;
import cn.calm.swing.osgi.api.ProjectService;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ProjectPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JLabel imgLb;
	private JLabel nameLb;
	private ProjectService projectService;
	private Boolean frist=true;
	private Border defaultBorder =BorderFactory.createMatteBorder(1, 1, 1, 1, new java.awt.Color(134,136,51));
	private BundleContext context;
	
	/**
	 * 
	 * @param projectService
	 * @param context
	 */
	public ProjectPanel(ProjectService projectService,BundleContext context) {
		this.projectService=projectService;
		this.context=context;
		initGUI();
		initDatabinding();
	}
	
	private void initDatabinding() {
		
	}

	private void initGUI() {
		try {
			//设置panel大小
			this.setPreferredSize(new java.awt.Dimension(110, 120));
			this.setBorder(defaultBorder);
			this.addMouseListener(new MouseAdapter() {
				public void mousePressed(MouseEvent e) {
					//右键
					actionRightClick(e);
				}
				public void mouseExited(MouseEvent evt) {
					//鼠标退出时
					ProjectPanel.this.setBorder(defaultBorder);
				}
				public void mouseEntered(MouseEvent evt) {
					ProjectPanel.this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
				}
			});
			{
				imgLb = new JLabel();
				this.add(imgLb);
				imgLb.setIcon(projectService.getIcon());
				imgLb.setHorizontalAlignment(SwingConstants.CENTER);
				imgLb.setPreferredSize(new java.awt.Dimension(86, 84));
			}
			{
				nameLb = new JLabel();
				this.add(nameLb);
				nameLb.setText(projectService.getProject().getName());
				nameLb.setHorizontalAlignment(SwingConstants.CENTER);
				nameLb.setPreferredSize(new java.awt.Dimension(100, 25));
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	

	
	private void actionRightClick(MouseEvent e) {
		int mods=e.getModifiers();
			//鼠标右键
		if((mods&InputEvent.BUTTON3_MASK)!=0){
			//弹出菜单
			JPopupMenu popupMenu1=projectService.getPopupMenu();
			if(popupMenu1==null){
				popupMenu1=new JPopupMenu();
			}
			if(frist){
				frist=false;
				popupMenu1.addSeparator();
				JMenuItem delete=new JMenuItem("删除");
				delete.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						int showConfirmDialog = JOptionPane.showConfirmDialog(ProjectPanel.this, "你确定要删除?", "系统消息", JOptionPane.OK_OPTION);
						if(showConfirmDialog==JOptionPane.OK_OPTION){
							ServiceReference<ProjectPersistenceService> serviceReference = context.getServiceReference(ProjectPersistenceService.class);
							ProjectPersistenceService service = context.getService(serviceReference);
							service.delete(projectService.getProject());
							ServiceRegistration<ProjectService> serviceRegistration = ProjectService.projectServiceRegistration.get(projectService.getProject().getId());
							if(serviceRegistration != null){
								serviceRegistration.unregister();
							}
							ProjectService.projectServiceRegistration.remove(projectService.getProject().getId());
							context.ungetService(serviceReference);
						}
						
					}
				});
				popupMenu1.add(delete);
			}
			popupMenu1.show(ProjectPanel.this,e.getX(),e.getY());
		}
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
}
